/**
 * @(#) DExpenseListProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 계약현황 조회 - Proxy
 *
 */
package com.skt.common_hr.holotreq.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.holotreq.model.vo.ReqDExpenseVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class HolOtReqProxy extends CommonProxy
	{
		public static const NAME:String = "HolOtReqProxy";



		public static const LOAD_WORKTYPECODE_COMPLETE:String = "LOAD_WORKTYPECODE_COMPLETE";
		public static const LOAD_WORKTYPECODE_FAILED:String = "LOAD_WORKTYPECODE_FAILED";

		public static const LOAD_DINNER_COMPLETE:String = "loadDinnerComplete";
		public static const LOAD_DINNER_FAILED:String = "loadDinnerFailed";
		public static const SAVE_DINNER_COMPLETE:String = "saveDinnerComplete";
		public static const SAVE_DINNER_FAILED:String = "saveDinnerFailed";
		public static const DEL_DINNER_COMPLETE:String = "delDinnerComplete";
		public static const DEL_DINNER_FAILED:String = "delDinnerFailed";

		public var key:String;
		public var dp:ReqDExpenseVO;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function HolOtReqProxy(data:Object=null)
		{
			super(NAME, data);
		}


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param pernr 사번
		 * @return void
		 */
		public function loadList(pernr:String, begda:Date, endda:Date, status:String):void
		{
			var param:Object = new Object();
			//Alert.show("load : " + param["I_PERNR"] + "," + param["I_BEGDA"]);
			loadData("Z_HR_F001", param, result, fault);
		}

		public function load(key:String):void
		{
			var param:Object = new Object();
			param["I_GWAREKEY"] = key;

			loadData("Z_HR_F003", param, result, fault);
		}


		public function save(_param:Object):void
		{
/* 			var param:Object = new Object();
			param["I_PERNR"] = _param.I_PERNR;
			param["I_DATUM"] = _param.I_DATUM;
			param["I_WTYPE"] = _param.I_WTYPE;
			param["I_WWHAT"] = _param.I_WWHAT;
			param["I_WSTAT"] = _param.I_WSTAT;

			var tables:Object = new Object();
			
			tables["O_ITAB"] = table; */
			
			var tables:Object = new Object();
			saveData("Z_HR_0148", _param, tables, resultSave, faultSave);
		}

		public function del(key:String):void
		{
			var param:Object = new Object();
			param["I_GWAREKEY"] = key;
			param["I_CANCEL"] = "X";

			loadData("Z_HR_F002", param, resultDel, faultDel);
		}
		
		//사전근무지시 근무유형 가져오기. 
		public function workTypeCode():void
		{
			var param:Object = new Object();
			param["I_DNAME"] = "ZDWTYPE";

			loadData("Z_HR_0003", param, resultTypeCode, faultDel);
		}








		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////

		// 사전근무지시요청 
		public var acWorkType:ArrayCollection;
		public function resultTypeCode(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				var o:Object = new Object();
				o.TEXT = '선택하세요';
				o.CODE = '0'; 
				
				acWorkType = event.result.ITAB;
				acWorkType.addItemAt(o, 0);
				facade.sendNotification(LOAD_WORKTYPECODE_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_WORKTYPECODE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				dp = parseDinner(event.result.OTAB);
				facade.sendNotification(LOAD_DINNER_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_DINNER_FAILED, this);
			}
		}

		public function resultSave(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {				
				facade.sendNotification(HolOtReqProxy.DEL_DINNER_COMPLETE, this);
			}
			else {
				facade.sendNotification(SAVE_DINNER_FAILED, this);
			}
		}

		public function resultDel(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(DEL_DINNER_COMPLETE, this);
			}
			else {
				facade.sendNotification(DEL_DINNER_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_DINNER_FAILED, this);
		}

		private function faultSave(event:FaultEvent):void
		{
			facade.sendNotification(SAVE_DINNER_FAILED, this);
		}

		private function faultDel(event:FaultEvent):void
		{
			facade.sendNotification(DEL_DINNER_FAILED, this);
		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 공지사항 Value Object
		 */
		 private function parseDinner(src:ArrayCollection):ReqDExpenseVO
		{
			if(src.length>0) {
				var vo:ReqDExpenseVO = new ReqDExpenseVO();
				var rec:Object = src.getItemAt(0);

				vo.GWAREKEY	= rec["GWAREKEY"];
				vo.WSTATUS 	= rec["WSTATUS"];
				vo.PERNR 	= rec["PERNR"];
				vo.BEGDA 	= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.TEXT1 	= rec["TEXT1"];
				vo.TEXT2 	= rec["TEXT2"];
				return vo;
			}
			else
				return null;
		}
	}
}